package com.company.cloud.ad.modules.mgr.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.ad.modules.base.entity.AdInfo;
import com.company.cloud.ad.modules.base.entity.AdPositionInfo;
import com.company.cloud.ad.modules.base.entity.AdPositionRelInfo;
import com.company.cloud.ad.modules.base.service.AdInfoService;
import com.company.cloud.ad.modules.base.service.AdPositionInfoService;
import com.company.cloud.ad.modules.base.service.AdPositionRelInfoService;
import com.company.cloud.ad.modules.cache.service.AdCacheService;
import com.company.cloud.ad.modules.mgr.controller.req.AdMgrAllocationParam;
import com.company.cloud.ad.modules.mgr.controller.req.AdMgrSaveParam;
import com.company.cloud.ad.modules.mgr.controller.req.AdMgrUpdateParam;
import com.company.cloud.ad.modules.mgr.controller.req.AdPositionMgrAllocationParam;
import com.company.cloud.ad.modules.mgr.service.AdMgrService;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.LocalDateTimeUtils;


@Service
public class AdMgrServiceImpl implements AdMgrService {

	@Autowired
	private AdInfoService adInfoService;
	@Autowired
	private AdPositionRelInfoService adPositionRelInfoService;
	@Autowired
	private AdCacheService adCacheService;
	@Autowired
	private AdPositionInfoService adPositionInfoService;
	
	
	@Transactional
	@Override
	public AdInfo add(AdMgrSaveParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		AdInfo entity=new AdInfo();
		entity.setCompanyNo(params.getCompanyNo());
		entity.setTitle(params.getTitle());
		entity.setContentType(params.getContentType());
		entity.setContent(params.getContent());
		entity.setUrl(params.getUrl());
		entity.setUrlParam(params.getUrlParam());
		if(!StringUtils.isEmpty(params.getBeginDate())) {
			entity.setBeginDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getBeginDate()));
		}
		if(!StringUtils.isEmpty(params.getEndDate())) {
			entity.setEndDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getEndDate()));
		}
		entity.setOrders(params.getOrders()==null?1:params.getOrders());
		entity.addCreateParam(userInfo);
		entity.addEditParam(userInfo);
		adInfoService.save(entity);
		
		if(StringUtils.isEmpty(params.getAdPositionNo())) {
			AdPositionRelInfo rel=new AdPositionRelInfo();
			rel.setCompanyNo(entity.getCompanyNo());
			rel.setAdId(entity.getId());
			rel.setAdPositionNo(params.getAdPositionNo());
			rel.addCreateParam(userInfo);
			rel.addEditParam(userInfo);
			adPositionRelInfoService.save(rel);
			adCacheService.removeAdByCache(params.getAdPositionNo());
		}
		
		return entity;
	}


	@Override
	public void edit(AdMgrUpdateParam params, LoginSimpleUserInfo userInfo) {
		AdInfo entity=adInfoService.getById(params.getId());
		if(entity == null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		entity.setTitle(params.getTitle());
		entity.setContentType(params.getContentType());
		entity.setContent(params.getContent());
		entity.setUrl(params.getUrl());
		entity.setUrlParam(params.getUrlParam());
		if(!StringUtils.isEmpty(params.getBeginDate())) {
			entity.setBeginDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getBeginDate()));
		}
		if(!StringUtils.isEmpty(params.getEndDate())) {
			entity.setEndDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getEndDate()));
		}
		entity.setOrders(params.getOrders()==null?1:params.getOrders());
		entity.addEditParam(userInfo);
		adInfoService.updateById(entity);
		
		List<AdPositionRelInfo> relList=adPositionRelInfoService.list(
				new QueryWrapper<AdPositionRelInfo>().eq("ad_id", entity.getId()));
		if(!CollectionUtils.isEmpty(relList)) {
			Set<String> positionNos=new HashSet<String>();
			relList.forEach(item->{
				positionNos.add(item.getAdPositionNo());
			});
			adCacheService.removeAdByCache(positionNos);
		}
		
	}


	@Override
	public void del(Integer id, LoginSimpleUserInfo userInfo) {
		AdInfo entity=adInfoService.getById(id);
		if(entity == null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		entity.setRowsState(BaseConstant.EnabledEnum.DISABLED.getCode());
		entity.addEditParam(userInfo);
		adInfoService.updateById(entity);

		List<AdPositionRelInfo> relList=adPositionRelInfoService.list(
				new QueryWrapper<AdPositionRelInfo>().eq("ad_id", entity.getId()));
		if(!CollectionUtils.isEmpty(relList)) {
			Set<String> positionNos=new HashSet<String>();
			relList.forEach(item->{
				positionNos.add(item.getAdPositionNo());
			});
			adCacheService.removeAdByCache(positionNos);
			adPositionRelInfoService.remove(new  QueryWrapper<AdPositionRelInfo>()
					.eq("ad_id", entity.getId()));
		}
		
		
	}


	@Override
	public void allocation(AdPositionMgrAllocationParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		AdPositionInfo adPosition=adPositionInfoService.getById(params.getAdPositionNo());
		if(adPosition==null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		
		Collection<AdInfo> adList=adInfoService.listByIds(params.getAdIds());

		List<AdPositionRelInfo> saveRel=new ArrayList<AdPositionRelInfo>();
		Set<String> companyNos=new HashSet<String>();
		if(!CollectionUtils.isEmpty(adList)) {
			for(AdInfo ad:adList) {
				AdPositionRelInfo model=new AdPositionRelInfo();
				model.setCompanyNo(ad.getCompanyNo());
				model.setAdId(ad.getId());
				model.setAdPositionNo(adPosition.getAdPositionNo());
				model.addEditParam(userInfo);
				model.addCreateParam(userInfo);
				saveRel.add(model);
				companyNos.add(ad.getCompanyNo());
			}
		}

		adPositionRelInfoService.remove(new  QueryWrapper<AdPositionRelInfo>()
				.eq("ad_position_no", params.getAdPositionNo())
				.in("company_no", companyNos)
				);
		
		adCacheService.removeAdByCache(params.getAdPositionNo());
		if(saveRel.size()>0) {
			adPositionRelInfoService.saveBatch(saveRel);
		}
	}


	@Override
	public void allocation(AdMgrAllocationParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		AdInfo ad=adInfoService.getById(params.getAdId());
		if(ad==null ||  BaseConstant.EnabledEnum.DISABLED.getCode().equals(ad.getRowsState()) ) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		
		List<AdPositionRelInfo> saveRel=new ArrayList<AdPositionRelInfo>();
		if(!CollectionUtils.isEmpty(params.getAdPositionNos())) {
			params.getAdPositionNos().forEach(adPositionNo->{
				AdPositionRelInfo model=new AdPositionRelInfo();
				model.setCompanyNo(ad.getCompanyNo());
				model.setAdId(ad.getId());
				model.setAdPositionNo(adPositionNo);
				model.addEditParam(userInfo);
				model.addCreateParam(userInfo);
				saveRel.add(model);
			});
		}
		
		List<AdPositionRelInfo> relList=adPositionRelInfoService.list(
				new QueryWrapper<AdPositionRelInfo>().eq("ad_id", params.getAdId()));
		if(!CollectionUtils.isEmpty(relList)) {
			Set<String> positionNos=new HashSet<String>();
			relList.forEach(item->{
				positionNos.add(item.getAdPositionNo());
			});
			adCacheService.removeAdByCache(positionNos);
			adPositionRelInfoService.remove(new  QueryWrapper<AdPositionRelInfo>()
					.eq("ad_id", params.getAdId()));
		}
		if(saveRel.size()>0) {
			adPositionRelInfoService.saveBatch(saveRel);
		}
	}

	
	
	
}
